package com.jerei.plugin.control;

import com.jerei.plugin.api.ILuceneService;
import com.jerei.utils.lucene.PageBeanForLucene;
import com.jerei.web.plugin.controller.PluginController;
import com.jerei.web.utils.UtilsWeb;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import ro.fortsoft.pf4j.Extension;

import java.util.*;

/**
 * Created by sunfeihu on 2016/12/27.
 */
@Extension
public class SiteSearchControl extends PluginController {

    @Autowired
    private ILuceneService iLuceneService;

    @RequiresPermissions("*:*")
    public void index() {
        List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", 1);
        map.put("name", "名称1");
        map.put("content", "内容1");
        map.put("pub_date", Calendar.getInstance().getTime());
        datas.add(map);
        map = new HashMap<String, Object>();
        map.put("id", 2);
        map.put("name", "名称2");
        map.put("content", "内容2");
        map.put("pub_date", Calendar.getInstance().getTime());
        datas.add(map);
        map = new HashMap<String, Object>();
        map.put("id", 3);
        map.put("name", "名称3");
        map.put("content", "内容3");
        map.put("pub_date", Calendar.getInstance().getTime());
        datas.add(map);
        iLuceneService.index(UtilsWeb.getString(this.getRequest(), "category"), datas);
    }

    @RequiresPermissions("*:*")
    public void search() {
        String[] fields = UtilsWeb.getString(this.getRequest(), "fields").split("'");
        PageBeanForLucene pageBeanForLucene = new PageBeanForLucene();
        pageBeanForLucene.setFields(fields);
        pageBeanForLucene.setCategory(UtilsWeb.getString(this.getRequest(), "category"));
        pageBeanForLucene.setKeywords(UtilsWeb.getString(this.getRequest(), "keywords"));
        pageBeanForLucene.setNowPage(UtilsWeb.getInteger(this.getRequest(), "nowPage"));
        pageBeanForLucene.setPageSize(UtilsWeb.getInteger(this.getRequest(), "pageSize"));
        pageBeanForLucene = iLuceneService.search(pageBeanForLucene);
        this.getModel().addAttribute("pb", pageBeanForLucene);
    }

}